<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung.
    *
    */

	class helper_text
	{
		public function __construct()
        {
        }

        public function limited_character($str, $length = 500, $end_char = '...')
        {

            if(strlen($str) < $length)
            {
                return $str;
            }

            $str = str_replace(array("\r\n", "\r", "\n"), ' ', $str);

            if(strlen($str) <= $length)
            {
                return $str;
            }

            $out = "";

            foreach (explode(' ', trim($str)) as $val)
            {
                $out .= $val.' ';

                if(strlen($out) >= $length)
                {
                    $out = trim($out);

                    return (strlen($out) == strlen($str) ? $out : $out.$end_char);
                }
            }


        }//End function limited_character()

        public function limited_word($str, $limit = 100, $end_char = '...')
        {
            /*Stripos: return position of param(2)*/
            if(stripos($str," "))
            {
                $ex_str = explode(" ",$str);

                if(count($ex_str) > $limit)
                {
                    $str_s = '';
                    for($i = 0; $i<$limit; $i ++)
                    {
                        $str_s.= $ex_str[$i]." ";
                    }

                    return $str_s.$end_char;
                }
                else
                {
                    return $str;
                }
            }
            else
            {
                return $str;
            }
        }//End function limted_word()

	}


    /*End of helper_text.php*/
    /*Location: my_framework/helpers/helper_text.php */